#include "mainwidget.h"

//SummWidget:
SummWidget::SummWidget(QWidget * parent,
	const QString& newBut,
	const QString& delBut)
	:StackList<QLineEdit*>(parent,newBut,delBut)
{
	Lang=new QLabel(tr("Language:"),this);
	Lang->setStatusTip(tr("e.g. zh_CN"));
	Summary=new QLabel(tr("Summary:"),this);
	Summary->setStatusTip(tr("one line brief introduction"));
	QLineEdit* temp=new QLineEdit(this);
	temp->setFixedWidth(50);
	temp->setMaxLength(16);
	lang<<temp;
	list<<new QLineEdit(this);
	connect(newbut,SIGNAL(released()),this,SLOT(ADD()));
	connect(delbut,SIGNAL(released()),this,SLOT(DEL()));
	update();
}

void SummWidget::update()
{
	delete glayout;
	glayout=new QGridLayout(this);
	glayout->addWidget(Lang,0,0);
	glayout->addWidget(Summary,0,1);
	for(int i=1;i<list.size()+1;i++)
	{
		glayout->addWidget(lang[i-1],i,0);
		glayout->addWidget(list[i-1],i,1);
	}
	glayout->addWidget(newbut,list.size()+1,0);
	glayout->addWidget(delbut,list.size()+1,1);
	setLayout(glayout);
}

void SummWidget::ADD()
{
	QLineEdit* temp=new QLineEdit(this);
	temp->setFixedWidth(50);
	temp->setMaxLength(16);
	lang<<temp;
	list<<new QLineEdit(this);
	update();
}

void SummWidget::DEL()
{
	if(list.size()>1)
	{
		delete lang.last();
		lang.pop_back();
		delete list.last();
		list.pop_back();
		update();
	}
}

SummWidget::~SummWidget()
{
	for(int i=lang.size()-1;i>=0;i--)
	{
		delete lang[i];
	}
	delete Lang;
	delete Summary;
}

const QString SummWidget::commit()
{
	QString rtv("");
	for(int i=0;i<lang.size();i++)
	{
		if(lang[i]->text().isEmpty()&&list[i]->text().isEmpty())
			continue;
		else if(lang[i]->text().isEmpty())
			rtv+="Summary :\t"+list[i]->text()+"\n";
		else
			rtv+="Summary ("+lang[i]->text()+"):\t"+list[i]->text()+"\n";
	}
	return rtv;
}


//SrcWidget:
SrcWidget::SrcWidget(QWidget * parent,
	const QString& str,
	const QString& newBut,
	const QString& delBut)
	:StackList<QLineEdit*>(parent,newBut,delBut),Str(str)
{
	list<<new QLineEdit(this);
	connect(newbut,SIGNAL(released()),this,SLOT(ADD()));
	connect(delbut,SIGNAL(released()),this,SLOT(DEL()));
	update();
}

void SrcWidget::update()
{
	delete glayout;
	glayout=new QGridLayout(this);
	for(int i=0;i<list.size();i++)
	{
		glayout->addWidget(list[i],i,0,1,2);
	}
	glayout->addWidget(newbut,list.size(),0);
	glayout->addWidget(delbut,list.size(),1);
	setLayout(glayout);
}

void SrcWidget::ADD()
{
	list<<new QLineEdit(this);
	update();
}

void SrcWidget::DEL()
{
	if(list.size()>1)
	{
		delete list.last();
		list.pop_back();
	}
	update();
}

const QString SrcWidget::commit()
{
	QString rtv("");
	for(int i=0;i<list.size();i++)
	{
		if(list[i]->text().isEmpty())
			continue;
		else
			rtv+=Str+QString::number(i)+":\t"+list[i]->text()+"\n";
	}
	return rtv;
}

//DescWidget:
DescWidget::DescWidget(QWidget * parent,
	const QString& newBut,
	const QString& delBut)
:StackList<QTextEdit*>(parent,newBut,delBut)
{
	QLineEdit* temp=new QLineEdit(this);
	temp->setFixedWidth(50);
	temp->setMaxLength(16);
	temp->setStatusTip(("e.g. zh_CN"));
	lang<<temp;
	list<<new QTextEdit(this);
	update();
	connect(newbut,SIGNAL(released()),this,SLOT(ADD()));
	connect(delbut,SIGNAL(released()),this,SLOT(DEL()));
}

DescWidget::~DescWidget()
{
	for(int i=lang.size()-1;i>=0;i--)
	{
		delete lang[i];
	}
}

void DescWidget::ADD()
{
	QLineEdit* temp=new QLineEdit(this);
	temp->setFixedWidth(50);
	temp->setMaxLength(16);
	temp->setStatusTip(("Language:\ne.g. zh_CN"));
	lang<<temp;
	list<<new QTextEdit(this);
	update();
}

void DescWidget::DEL()
{
	if(lang.size()>1)
	{
		delete lang.last();
		lang.pop_back();
		delete list.last();
		list.pop_back();
		update();
	}
}

void DescWidget::update()
{
	delete glayout;
	glayout=new QGridLayout(this);
	for(int i=0;i<lang.size();i++)
	{
		glayout->addWidget(lang[i],i*2,0,1,2);
		glayout->addWidget(list[i],i*2+1,0,1,2);
	}
	glayout->addWidget(newbut,lang.size()*2,0);
	glayout->addWidget(delbut,lang.size()*2,1);
	setLayout(glayout);
}

const QString DescWidget::commit()
{
	QString rtv("");
	for(int i=0;i<lang.size();i++)
	{
		if(lang[i]->text().isEmpty()&&list[i]->toPlainText().isEmpty())
			continue;
		else if(lang[i]->text().isEmpty())
			rtv+="%description\n"+list[i]->toPlainText()+"\n";
		else
			rtv+="%description -l "+lang[i]->text()+"\n"+list[i]->toPlainText()+"\n";
	}
	return rtv;
}


//HeadWidget:
HeadWidget::HeadWidget(QWidget * parent)
	:QGroupBox(parent)
{
	glayout		=new QGridLayout(this);
		name		=new QLabel(tr("&Name"),this);
		summary		=new QLabel(tr("&Summary"),this);
		version		=new QLabel(tr("&Version"),this);
		release		=new QLabel(tr("&Release"),this);
		license		=new QLabel(tr("&License"),this);
		group		=new QLabel(tr("&Group"),this);
		source		=new QLabel(tr("S&ource"),this);
		patch		=new QLabel(tr("&Patch"),this);
		requires	=new QLabel(tr("Re&quires"),this);
		brequires	=new QLabel(tr("B&uildRequires"),this);
		buildroot	=new QLabel(tr("&Build Root"),this);
		buildroot	->setStatusTip(tr("the RPM_BUILD_ROOT environment variable"));
		description	=new QLabel(tr("&Description"),this);
	
		Name		=new QLineEdit(this);
		name		->setBuddy(Name);
		Summary		=new SummWidget(this);
		summary		->setBuddy(Summary);
		Version		=new QLineEdit(this);
		version		->setBuddy(Version);
		Release		=new QLineEdit(this);
		release		->setBuddy(Release);
		License		=new QLineEdit(this);
		license		->setBuddy(License);
		Group		=new QComboBox(this);
		group		->setBuddy(Group);
		init_Group();
		Source		=new SrcWidget(this);
		source		->setBuddy(Source);
		Patch		=new SrcWidget(this,"Patch");
		patch		->setBuddy(Patch);
		Requires	=new QLineEdit(this);
		requires	->setBuddy(Requires);
		BRequires	=new QLineEdit(this);
		brequires	->setBuddy(BRequires);
		BuildRoot	=new QLineEdit(this);
		buildroot	->setBuddy(BuildRoot);
		Description	=new DescWidget(this);
		description	->setBuddy(Description);
		
		int i=0;
		glayout->addWidget(name,i,0);			glayout->addWidget(Name,i++,1);
		glayout->addWidget(summary,i,0);		glayout->addWidget(Summary,i++,1);
		glayout->addWidget(version,i,0);		glayout->addWidget(Version,i++,1);
		glayout->addWidget(release,i,0);		glayout->addWidget(Release,i++,1);
		glayout->addWidget(license,i,0);		glayout->addWidget(License,i++,1);
		glayout->addWidget(group,i,0);			glayout->addWidget(Group,i++,1);
		glayout->addWidget(source,i,0);			glayout->addWidget(Source,i++,1);
		glayout->addWidget(patch,i,0);			glayout->addWidget(Patch,i++,1);
		glayout->addWidget(requires,i,0);		glayout->addWidget(Requires,i++,1);
		glayout->addWidget(brequires,i,0);		glayout->addWidget(BRequires,i++,1);
		glayout->addWidget(buildroot,i,0);		glayout->addWidget(BuildRoot,i++,1);
		glayout->addWidget(description,i++,0);	glayout->addWidget(Description,i++,0,1,2);
	setLayout(glayout);
}

void HeadWidget::init_Group()
{
	QFile groupfile(":/files/GROUPS");
	if (!groupfile.open(QIODevice::ReadOnly | QIODevice::Text))
		qDebug()<<"Can't Open file: GROUPS";
	QTextStream in(&groupfile);
	QStringList list;
	while(!in.atEnd())
	{
		list<<in.readLine(100);
	}
	Group->insertItems(0,list);
	groupfile.close();
}

const QString HeadWidget::commit()
{
	QString rtv("");
	rtv+=(Name->text().isEmpty())?("")
		:("Name :\t"+Name->text()+"\n");
	rtv+=Summary->commit();
	rtv+=(Version->text().isEmpty())?("")
		:("Version :\t"+Version->text()+"\n");
	rtv+=(Release->text().isEmpty())?("")
		:("Release :\t"+Release->text()+"\n");
	rtv+=(License->text().isEmpty())?("")
		:("License :\t"+License->text()+"\n");
	rtv+="Group :\t"+Group->itemText(Group->currentIndex())+"\n";
	rtv+=Source->commit();
	rtv+=Patch->commit();
	rtv+=(Requires->text().isEmpty())?("")
		:("Requires :\t"+Requires->text()+"\n");
	rtv+=(BRequires->text().isEmpty())?("")
		:("BuildRequires :\t"+BRequires->text()+"\n");
	rtv+=(BuildRoot->text().isEmpty())?("")
		:("BuildRoot :\t"+BuildRoot->text()+"\n");
	rtv+=Description->commit();
	rtv+="\n";
	return rtv;
}

//ProcedureWidget:
ProcedureWidget::ProcedureWidget(QWidget* parent)
	:QGroupBox(tr("Scripts:"),parent)
{
	vlayout0		=new QVBoxLayout(this);
	vlayout1		=new QVBoxLayout;
	vlayout2		=new QVBoxLayout;
	hlayout0		=new QHBoxLayout;
	hlayout1		=new QHBoxLayout;
	prep		=new QLabel(tr("Prepare (%prep):"),this);
	prep		->setStatusTip(tr("prepare script"));
	prep		->setToolTip(tr("prepare for build\ne.g. %setup -q\n./configure CFLAGS=\"$RPM_OPT_FLAGS\" CPPFLAGS=\"$RPM_OPT_FLAGS\""));
	build		=new QLabel(tr("Build (%build):"),this);
	build		->setStatusTip(tr("build script"));
	build		->setToolTip(tr("build\ne.g. make %{_smp_mflags}"));
	install		=new QLabel(tr("Install (%install):"),this);
	install		->setStatusTip(tr("install script"));
	install		->setToolTip(tr("install\ne.g. make DESTDIR=\"$RPM_BUILD_ROOT\" install"));
	clean		=new QLabel(tr("Clean (%clean):"),this);
	clean		->setStatusTip(tr("clean script"));
	clean		->setToolTip(tr("clean\ne.g. rm -rf \"$RPM_BUILD_ROOT\""));
	pre 		=new QLabel(tr("pre-install (%pre):"),this);
	pre 		->setStatusTip(tr("before install script"));
	post		=new QLabel(tr("post-install (%post):"),this);
	post		->setStatusTip(tr("after install script"));
	preun		=new QLabel(tr("pre-erase (%preun):"),this);
	preun		->setStatusTip(tr("before erase script"));
	postun		=new QLabel(tr("post-erase (%postun):"),this);
	postun		->setStatusTip(tr("after erase script"));
	Prep		=new QTextEdit(this);
	Build		=new QTextEdit(this);
	Install		=new QTextEdit(this);
	Clean		=new QTextEdit(this);
	Pre 		=new QTextEdit(this);
	Post		=new QTextEdit(this);
	Preun		=new QTextEdit(this);
	Postun		=new QTextEdit(this);
	
	buildBox	=new QCheckBox(tr("Build Scripts"),this);
	installBox	=new QCheckBox(tr("Install/Erase Scripts"),this);
	connect(buildBox,SIGNAL(stateChanged(int)),this,SLOT(update(int)));
	connect(installBox,SIGNAL(stateChanged(int)),this,SLOT(update(int)));
	hlayout1		->addWidget(buildBox);
	hlayout1		->addWidget(installBox);
	vlayout0	->addLayout(hlayout1);
	vlayout1	->addWidget(prep);
	vlayout1	->addWidget(Prep);
	vlayout1	->addWidget(build);
	vlayout1	->addWidget(Build);
	vlayout1	->addWidget(install);
	vlayout1	->addWidget(Install);
	vlayout1	->addWidget(clean);
	vlayout1	->addWidget(Clean);
	vlayout2	->addWidget(pre);
	vlayout2	->addWidget(Pre);
	vlayout2	->addWidget(post);
	vlayout2	->addWidget(Post);
	vlayout2	->addWidget(preun);
	vlayout2	->addWidget(Preun);
	vlayout2	->addWidget(postun);
	vlayout2	->addWidget(Postun);
	vlayout0	->addLayout(vlayout1);
	vlayout0	->addLayout(vlayout2);
	update();
	setLayout(vlayout0);
}

void ProcedureWidget::update(int)
{
	if(buildBox->checkState()!=Qt::Checked)
	{
		for(int i=0;i<vlayout1->count();i++)
			vlayout1->itemAt(i)->widget()->hide();
	}
	else
	{
		for(int i=0;i<vlayout1->count();i++)
			vlayout1->itemAt(i)->widget()->show();
	}
	if(installBox->checkState()!=Qt::Checked)
	{
		for(int i=0;i<vlayout1->count();i++)
			vlayout2->itemAt(i)->widget()->hide();
	}
	else
	{
		for(int i=0;i<vlayout1->count();i++)
			vlayout2->itemAt(i)->widget()->show();
	}
}

const QString ProcedureWidget::commit()
{
	QString rtv("");
	if(buildBox->checkState()==Qt::Checked)
	{
		rtv+=(Prep->toPlainText().isEmpty())?("")
			:("%prep\n"+Prep->toPlainText()+"\n");
		rtv+=(Build->toPlainText().isEmpty())?("")
			:("%build\n"+Build->toPlainText()+"\n");
		rtv+=(Install->toPlainText().isEmpty())?("")
			:("%install\n"+Install->toPlainText()+"\n");
		rtv+=(Clean->toPlainText().isEmpty())?("")
			:("%clean\n"+Clean->toPlainText()+"\n");
	}
	if(installBox->checkState()==Qt::Checked)
	{
		rtv+=(Pre->toPlainText().isEmpty())?("")
			:("%pre\n"+Pre->toPlainText()+"\n");
		rtv+=(Post->toPlainText().isEmpty())?("")
			:("%post\n"+Post->toPlainText()+"\n");
		rtv+=(Preun->toPlainText().isEmpty())?("")
			:("%preun\n"+Preun->toPlainText()+"\n");
		rtv+=(Postun->toPlainText().isEmpty())?("")
			:("%postun\n"+Postun->toPlainText()+"\n");
	}
	rtv+="\n";
	return rtv;
}

//FileWidget:
FileWidget::FileWidget(QWidget * parent)
	:QGroupBox(tr("File List (%files):"),parent)
{
	glayout=new QGridLayout(this);
	list=new QTextEdit(this);
	glayout->addWidget(list,1,0);
	setLayout(glayout);
}

const QString FileWidget::commit()
{
	QString rtv("");
	rtv+=(list->toPlainText().isEmpty())?("")
		:("%files\n"+list->toPlainText()+"\n");
	rtv+="\n";
	return rtv;
}

ChangeLogWidget::ChangeLogWidget(QWidget * parent)
	:QGroupBox(tr("Change Log(%changelog):"),parent)
{
	glayout=new QGridLayout(this);
	date=new QDateEdit(this);
	date->setDate(QDate::currentDate());
	info=new QLineEdit(this);
	log=new QTextEdit(this);
	glayout->addWidget(date,0,0);
	glayout->addWidget(info,0,1);
	glayout->addWidget(log,1,0,1,2);
	setLayout(glayout);
}

const QString ChangeLogWidget::commit()
{
	QString rtv=("");
	rtv+=(info->text().isEmpty())?(""):
		("%changelog\n* "+QLocale::c().toString(date->date(),QString("ddd MMM dd yyyy"))+
		"\t"+info->text()+"\n- "+log->toPlainText()+"\n");
	return rtv;
}


MainWidget::MainWidget(const QString & title, QWidget * parent, Qt::WindowFlags flags)
	:QDockWidget(title,parent,flags)
{
	setAllowedAreas(Qt::AllDockWidgetAreas);
	scroll=new QScrollArea(this);
	scroll->setWidgetResizable(true);
	content=new QWidget(this);
		vlayout=new QVBoxLayout(content);
		head=new HeadWidget(content);
		proc=new ProcedureWidget(content);
		file=new FileWidget(content);
		changelog=new ChangeLogWidget(content);
		vlayout->addWidget(head);
		vlayout->addWidget(proc);
		vlayout->addWidget(file);
		vlayout->addWidget(changelog);
		content->setLayout(vlayout);
	scroll->setWidget(content);
	setWidget(scroll);
}

const QString MainWidget::commit()
{
	QString rtv("");
	rtv+="#Generated By Evpacker\n";
	rtv+=head->commit();
	rtv+=proc->commit();
	rtv+=file->commit();
	rtv+=changelog->commit();
	return rtv;
}


